package com.gilstar.gilstarwebview;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity 
{
	
	private WebView wb;
	
	private ValueCallback<Uri> mUploadMessage;
	
	private final static int FILECHOOSER_RESULTCODE=1;  
	   
	 @Override  
	 protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
	 {  
		 if(requestCode==FILECHOOSER_RESULTCODE)  
		 {  
			 if (null == mUploadMessage) return;  
			 Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();  
			 mUploadMessage.onReceiveValue(result);  
			 mUploadMessage = null;  
		 }  
	 }  
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        this.wb = (WebView)findViewById(R.id.webView1);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setGeolocationEnabled(true);
        wb.getSettings().setAllowFileAccess(true);
        wb.loadUrl("http://gilstar.com");
        wb.setWebViewClient(new WebViewClientClass()); 
        wb.setWebChromeClient(new WebChromeClient()
        {
        	public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) 
        	{
        		callback.invoke(origin, true, false);
        	};
        	
        	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) 
        	{  
        		mUploadMessage = uploadMsg;  
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );  
                  
        	}
        	 public void openFileChooser( ValueCallback<Uri> uploadMsg ) 
        	 {
        		 openFileChooser( uploadMsg, "" );
        	 }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wb.canGoBack()) { 
            wb.goBack(); 
            return true; 
        } 
        return super.onKeyDown(keyCode, event);
    }
    
    private class WebViewClientClass extends WebViewClient 
    { 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        { 
            view.loadUrl(url); 
            return true; 
        }
    }
}
